]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - Super Polarity/ActorManager.cs
Implements polarity system
[rbdr/super-polarity] / Super Polarity / ActorManager.cs
... / ...
CommitLineData
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Graphics;
7
8namespace SuperPolarity
9{
10 static class ActorManager
11 {
12 static List<Actor> Actors;
13
14 static ActorManager()
15 {
16 Actors = new List<Actor>();
17 }
18
19 static public void CheckIn(Actor actor)
20 {
21 Actors.Add(actor);
22 }
23
24 static public void CheckOut(Actor actor)
25 {
26 Actors.Remove(actor);
27 }
28
29 static public void Update(GameTime gameTime)
30 {
31 CheckActors();
32 foreach (Actor actor in Actors)
33 {
34 actor.Update(gameTime);
35 }
36 }
37
38 static public void Draw(SpriteBatch spriteBatch)
39 {
40 foreach (Actor actor in Actors)
41 {
42 actor.Draw(spriteBatch);
43 }
44 }
45
46 static void CheckActors()
47 {
48 var i = 0;
49 foreach (Actor actor in Actors)
50 {
51 i++;
52 foreach (Actor other in Actors.Skip(i))
53 {
54 CheckCollision(actor, other);
55
56 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
57 {
58 CheckMagnetism((Ship)actor, (Ship)other);
59 }
60 }
61 }
62 }
63
64 static void CheckCollision(Actor actor, Actor other)
65 {
66
67 }
68
69 static void CheckMagnetism(Ship actor, Ship other)
70 {
71 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
72 {
73 var dy = other.Position.Y - actor.Position.Y;
74 var dx = other.Position.X - actor.Position.X;
75 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
76 var angle = (float) Math.Atan2(dy, dx);
77
78 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
79 {
80 actor.Magnetize(other, (float)linearDistance, angle);
81 other.Magnetize(actor, (float)linearDistance, 90 - angle);
82 }
83 }
84 }
85 }
86}